<template>
  <section class="f-agents">
    <div class="w-card my-content-box" v-if="!isDetail">
      <div class="title-box">
        <span class="title-text">代理商库存列表</span>
      </div>
      <div class="my-search-box">
        <div>
          <span>设备商品ID：</span>
          <el-input v-model="searchForm.wareGoodsId" clearable placeholder="请输入设备商品ID" />
        </div>
        <div>
          <span>设备型号：</span>
          <el-input v-model="searchForm.equipmentType" clearable placeholder="请输入型号" />
        </div>
        <div>
          <span>设备颜色：</span>
          <el-input v-model="searchForm.color" clearable placeholder="请输入颜色" />
        </div>
        <div>
          <span>代理商：</span>
          <el-select v-model="searchForm.shopId" filterable clearable placeholder="请选择">
            <el-option v-for="item in shopList" :key="item.shopId" :label="item.shopName" :value="item.shopId" />
          </el-select>
        </div>
        <div class="f-ml-20">
          <el-button @click="searchHandle" type="primary">查询</el-button>
          <el-button @click="resetHandle">重置</el-button>
        </div>
      </div>

      <div class="my-table-box">
        <el-table :height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
          :cell-style="{fontSize:'12px',color:'#7b7b7d'}" :data="tableData" stripe @selection-change="selectionChange">
          <!-- <el-table-column type="selection" width="55" /> -->
          <el-table-column type="index" width="55" label="序号" align="center" />
          <el-table-column prop="shopName" label="代理商" align="center">
            <template v-slot="scope">
              <el-link style="font-size:12px" type="primary" @click="lookDetail(scope.row)">
                {{ scope.row.shopName || '' }}
              </el-link>
            </template>
          </el-table-column>
          <el-table-column prop="wareGoodsId" label="设备商品ID" align="center">
            <template v-slot="scope">
              {{ scope.row.wareGoodsId || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="equipmentType" label="设备型号" align="center">
            <template v-slot="scope">
              {{ scope.row.equipmentType || '--' }}
            </template>
          </el-table-column>
          <el-table-column prop="color" label="设备颜色" align="center">
            <template v-slot="scope">
              {{ scope.row.color || '--' }}
            </template>
          </el-table-column>
          <el-table-column prop="activeTotal" label="已激活" align="center">
            <template v-slot="scope">
              {{ scope.row.activeTotal }}
            </template>
          </el-table-column>
          <el-table-column prop="rest" label="未激活" align="center">
            <template v-slot="scope">
              {{ scope.row.rest }}
            </template>
          </el-table-column>
          <el-table-column prop="createTimeMill" label="创建时间" align="center">
            <template v-slot="scope">
              {{ getDateTime(scope.row.createTimeMill)[6]  || '--' }}
            </template>
          </el-table-column>
        </el-table>
        <div class="my-table-footer">
          <!-- <div class="footer_btn_box">
            <el-button :disabled="chooseList.length == 0" @click="settingHandle" type="warning" size="small"
              plain>批量导出</el-button>
          </div> -->
          <el-pagination style="margin-left:auto;" v-model:currentPage="page" v-model:page-size="size"
            :page-sizes="[10, 20, 50, 100]" small :background="true" layout="total, sizes, prev, pager, next, jumper"
            :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
      </div>
    </div>

    <!-- 导入弹窗 -->
    <Upload ref="uploadRef" v-if="off.upload" :uploadForm="uploadForm" @handleUploadSuccess="importSuccess"
      @handleUploadCancel="handleUploadCancel" />

    <!-- 确认密码 -->
    <ConfirmPass ref="confirmPasswordRef" @verification="confirmSuccess" />

    <router-view></router-view>
  </section>
</template>

<script>
import { onMounted, reactive, ref, toRefs, watch, nextTick } from 'vue'
import { getWindowWidthAndHeight, warningDeal, getDateTime, successDeal, BlobDownLoad } from '@/utils/utils'
import { useRoute, useRouter } from 'vue-router'
import Upload from '@/components/Upload.vue'
import ConfirmPass from '@/components/confirmPass.vue'
import api from '../api'

export default {
  components: { Upload, ConfirmPass },

  setup() {
    const route = useRoute()
    const router = useRouter()

    const uploadRef = ref(null)
    const confirmPasswordRef = ref(null)

    const state = reactive({
      off: {
        upload: false,
      },
      isDetail: false,

      searchForm: {
        "color": "",
        "equipmentType": "",
        "orderBy": "id desc",
        "shopId": "",
        "wareGoodsId": ""
      },

      tableHeight: 0,
      tableData: [],
      page: 1,
      size: 10,
      total: 0,

      chooseList: [],
      shopList: [],

      uploadForm: {
        dialogTitle: '导入加盟订单',
        action: '/eas/manage/order/join/upload'
      }
    })

    watch(() => route.path, (newVal, oldVal) => {
      setIsDetail()
    })

    function setIsDetail() {
      state.isDetail = (route.path != '/inventoryAgents')
      getTableData()
    }

    function searchHandle() {
      state.page = 1
      getTableData()
    }

    function resetHandle() {
      state.searchForm = {
        "color": "",
        "equipmentType": "",
        "orderBy": "id desc",
        "shopId": "",
        "wareGoodsId": ""
      }
      searchHandle()
    }

    function getTableData() {
      const obj = {
        page: state.page,
        size: state.size,
        ...state.searchForm,
      }
      api.warehouseDeptList(obj).then(res => {
        state.tableData = res.data.list
        state.total = res.data.total
      }).catch(err => {
        console.log(err)
      })
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    function lookDetail(data) {
      router.push({
        path: '/inventoryAgentsDetail',
        query: {
          shopId: data.shopId,
          wareGoodsId: data.wareGoodsId,
          shopName: data.shopName,
          equipmentType: data.equipmentType,
        }
      })
    }

    // 导出
    const exportHandle = () => {
      const obj = {
        responseType: 'blob',
        ...state.searchForm,
      }
      api.deptDeviceExport(obj).then(res => {
        const _fileName = decodeURIComponent(res.headers['content-disposition'].split(';')[1].split('=')[1])
        BlobDownLoad(res, _fileName)
      }).catch(err => {
        console.log(err)
      })
    }

    // 导入
    function importHandle() {
      state.off.upload = true
      nextTick(() => {
        uploadRef.value.off.dialog = true
      })
    }

    //导入成功
    function importSuccess() {
      state.page = 1
      getTableData()
    }

    const handleUploadCancel = () => {
      state.off.upload = false
    }

    function settingHandle() {
      if (state.chooseList.length == 0) {
        return
      }
      // const title = "套餐产品删除提示"
      // const msg = '您确定要将所选的套餐产品删除吗？'
      // confirmPasswordRef.value.dialogShow(title, msg)
    }

    const confirmSuccess = () => { }

    function selectionChange(list) {
      state.chooseList = list
    }

    const getShopList = () => {
      api.shopList().then(res => {
        state.shopList = res.data.list
      })
    }

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 280
      setIsDetail()
      getShopList()
    })

    return {
      ...toRefs(state),
      uploadRef,
      confirmPasswordRef,
      searchHandle,
      resetHandle,
      lookDetail,
      handleCurrentChange,
      handleSizeChange,
      getDateTime,
      exportHandle,
      importSuccess,
      importHandle,
      handleUploadCancel,
      settingHandle,
      confirmSuccess,
      selectionChange,
    }
  }
}
</script>

<style lang="scss" scoped>
.f-agents {
  .my-content-box {
    box-sizing: border-box;
    padding: 20px;

    > .title-box {
      display: flex;
      align-items: center;

      > .title-text {
        font-size: 16px;
        font-weight: bold;
      }
    }

    .my-search-box {
      box-sizing: border-box;
      padding: 20px 20px 20px 0;
      padding-bottom: 0;
      font-size: 14px;
      display: flex;
      align-items: center;
      gap: 10px;

      > div {
        width: 300px;
        display: flex;
        align-items: center;

        > span {
          flex-shrink: 0;
          width: 90px;
          text-align: right;
        }
      }

      .datebox {
        width: 400px;
      }
    }

    .my-table-box {
      margin-top: 20px;

      .table-handle-box {
        display: flex;
        justify-content: center;
        align-items: center;
      }

      .my-table-footer {
        display: flex;
        align-items: center;
        margin-top: 10px;
        flex-wrap: wrap;
        gap: 10px;
      }
    }
  }
}
</style>